/** 
 * BuildCraft is open-source. It is distributed under the terms of the 
 * BuildCraft Open Source License. It grants rights to read, modify, compile
 * or run the code. It does *NOT* grant the right to redistribute this software
 * or its modifications in any form, binary or source, except if expressively
 * granted by the copyright holder.
 */

package aps.module_Fusion;

//import net.minecraft.src.*;

//vanilla minecraft
import net.minecraft.src.TileEntity;
import net.minecraft.src.NBTTagCompound;

import aps.BuildcraftAPS;
//buildcraft
import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.core.Orientations;
import buildcraft.api.core.Position;
import buildcraft.api.power.PowerFramework;
//import buildcraft.api.power.PowerProvider;
import buildcraft.api.power.IPowerProvider;
import buildcraft.core.DefaultProps;
import buildcraft.core.utils.Utils;
import buildcraft.transport.Pipe;
import buildcraft.transport.pipes.PipeLogicWood;
import buildcraft.transport.PipeTransportPower;
import buildcraft.transport.TileGenericPipe;

public class PipePowerLapis extends Pipe implements IPowerReceptor {

	private IPowerProvider powerProvider; //Updated to reflect new PowerFramework

	private int baseTexture = module_Fusion.TextureIdPipePowerLapis;
	private int plainTexture = module_Fusion.TextureIdPipePowerLapis;
	private int nextTexture = baseTexture;
	
	private float MovingAverage = 0;
	private int MovingAverageNodes = 160;

	public PipePowerLapis(int itemID) {
		super(new PipeTransportPower(), new PipeLogicWood(), itemID);

		powerProvider = PowerFramework.currentFramework.createPowerProvider();
		powerProvider.configure(0, 1, 500, 1, 500);
		powerProvider.configurePowerPerdition(1, 100);
	}
	
	//this function is no longer a part of Pipe - Seph
	//public int getMainBlockTexture() {
		//return nextTexture;
	//}
	
	//Added new override, getTextureIndex
	@Override
	public int getTextureIndex(Orientations direction)
	{
		//return nextTexture;
		//return 0;
		return 7 * 16 + 11;
	}

	@Override
	public String getTextureFile()
	{
		//return BuildcraftAPS.imageFilesRoot + "PipePowerLapis.png";
		return DefaultProps.TEXTURE_BLOCKS;
	}
	
	public String getName()
	{
		return "Advanced Power Output Pipe";
	}
	
	@Override
	public void setPowerProvider(IPowerProvider provider) { //updated to IPowerProvider to avoid mismatch
		provider = powerProvider;
	}

	@Override
	public IPowerProvider getPowerProvider() { //ditto
		return powerProvider;
	}

	@Override
	public void doWork() {
		// TODO Auto-generated method stub
	}

	@Override
	public void updateEntity() {
		//Buildcraft now handles this - Seph
		//if (powerProvider.getEnergyStored() >= 500)
			//powerProvider.energyStored = 500;
		
		super.updateEntity();
		
		
		//if (powerProvider.getEnergyStored() >= 500)
			//powerProvider. = 500;
		
		for (Orientations o : Orientations.dirs())
		{
			if (Utils.checkPipesConnections(container, container.getTile(o)))
			{
				TileEntity tile = container.getTile(o);

				if (tile instanceof TileGenericPipe)
				{
					PipeTransportPower pow = (PipeTransportPower) ((TileGenericPipe) tile).pipe.transport;

					float energyToRemove = 0;
					
					MovingAverage -= MovingAverage / MovingAverageNodes;
					MovingAverage += powerProvider.getEnergyStored();
					
					energyToRemove = MovingAverage / MovingAverageNodes;
					
					float energyUsed = powerProvider.useEnergy(1, energyToRemove, true);
					
					pow.receiveEnergy(o.reverse(), energyUsed);
					
					if(worldObj.isRemote) return; //only get power to display from server 
					((PipeTransportPower) transport).displayPower[o.ordinal()] += energyUsed;
				}
			}
		}
	}

	//removed - Seph
	/*public void prepareTextureFor(Orientations connection) {
		if (connection == Orientations.Unknown) {
			nextTexture = baseTexture;
		} else {
			int metadata = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);

			if (metadata == connection.ordinal()) {
				nextTexture = plainTexture;
			} else {
				nextTexture = baseTexture;
			}
		}
	}*/
	
	@Override
	public int powerRequest() {
		return getPowerProvider().getMaxEnergyReceived();
	}

	/*@Override
	public void writeToNBT(NBTTagCompound nbttagcompound)
	{
		nbttagcompound.setFloat("lapismovingaverage", MovingAverage);
		super.writeToNBT(nbttagcompound);
	}
	
	@Override
	public void readFromNBT(NBTTagCompound nbttagcompound)
	{
		MovingAverage = nbttagcompound.getFloat("lapismovingaverage");
		super.readFromNBT(nbttagcompound);
	}*/
}
